namespace CommontLibrary
{
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using System.Text;
    /// <summary>
    /// Summary description for FileUntil
    /// </summary>
    public class FileUntil
    {
        public FileUntil()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        public static string strFileExcel(string strView)
        {
            string strHTML = "<html xmlns:o=\"urn:schemas-microsoft-com:office:office\" ";
            strHTML += "\n xmlns:x=\"urn:schemas-microsoft-com:office:excel\" ";
            strHTML += "\n xmlns=\"http://www.w3.org/TR/REC-html40\"> ";
            strHTML += "\n <head> ";
            strHTML += "\n <meta http-equiv=Content-Type content=\"text/html; charset=UTF-8\"> ";
            strHTML += "\n <meta name=ProgId content=Excel.Sheet> ";
            strHTML += "\n <meta name=Generator content=\"Microsoft Excel 11\"> ";
            strHTML += "\n <link rel=File-List ";
            strHTML += "\n href=\"New%20Microsoft%20Excel%20Worksheet_files/filelist.xml\"> ";
            strHTML += "\n <link rel=Edit-Time-Data ";
            strHTML += "\n href=\"New%20Microsoft%20Excel%20Worksheet_files/editdata.mso\"> ";
            strHTML += "\n <link rel=OLE-Object-Data ";
            strHTML += "\n href=\"New%20Microsoft%20Excel%20Worksheet_files/oledata.mso\"> ";
            strHTML += "\n <!--[if gte mso 9]><xml> ";
            strHTML += "\n  <o:DocumentProperties> ";
            strHTML += "\n   <o:LastAuthor>User</o:LastAuthor> ";
            strHTML += "\n   <o:Created>1996-10-14T23:33:28Z</o:Created> ";
            strHTML += "\n   <o:LastSaved>2009-06-04T09:49:20Z</o:LastSaved> ";
            strHTML += "\n   <o:Version>11.5606</o:Version> ";
            strHTML += "\n </o:DocumentProperties> ";
            strHTML += "\n </xml><![endif]--> ";
            strHTML += "\n <style> ";
            strHTML += "\n <!--table ";
            strHTML += "\n      {mso-displayed-decimal-separator:\"\\.\"; ";
            strHTML += "\n      mso-displayed-thousand-separator:\"\\,\";} ";
            strHTML += "\n @page ";
            strHTML += "\n      {margin:1.0in .75in 1.0in .75in; ";
            strHTML += "\n      mso-header-margin:.5in; ";
            strHTML += "\n 	mso-footer-margin:.5in;} ";
            strHTML += "\n tr ";
            strHTML += "\n 	{mso-height-source:auto;} ";
            strHTML += "\n col ";
            strHTML += "\n 	{mso-width-source:auto;} ";
            strHTML += "\n br ";
            strHTML += "\n 	{mso-data-placement:same-cell;} ";
            strHTML += "\n .style0 ";
            strHTML += "\n 	{mso-number-format:General; ";
            strHTML += "\n 	text-align:general; ";
            strHTML += "\n 	vertical-align:bottom; ";
            strHTML += "\n 	white-space:nowrap; ";
            strHTML += "\n 	mso-rotate:0; ";
            strHTML += "\n 	mso-background-source:auto; ";
            strHTML += "\n 	mso-pattern:auto; ";
            strHTML += "\n 	color:windowtext; ";
            strHTML += "\n 	font-size:10.0pt; ";
            strHTML += "\n 	font-weight:400; ";
            strHTML += "\n 	font-style:normal; ";
            strHTML += "\n 	text-decoration:none; ";
            strHTML += "\n 	font-family:Arial; ";
            strHTML += "\n 	mso-generic-font-family:auto; ";
            strHTML += "\n 	mso-font-charset:0; ";
            strHTML += "\n 	border:none; ";
            strHTML += "\n 	mso-protection:locked visible; ";
            strHTML += "\n 	mso-style-name:Normal; ";
            strHTML += "\n 	mso-style-id:0;} ";
            strHTML += "\n td ";
            strHTML += "\n 	{mso-style-parent:style0; ";
            strHTML += "\n 	padding-top:1px; ";
            strHTML += "\n 	padding-right:1px; ";
            strHTML += "\n 	padding-left:1px; ";
            strHTML += "\n 	mso-ignore:padding; ";
            strHTML += "\n 	color:windowtext; ";
            strHTML += "\n 	font-size:10.0pt; ";
            strHTML += "\n 	font-weight:400; ";
            strHTML += "\n 	font-style:normal; ";
            strHTML += "\n 	text-decoration:none; ";
            strHTML += "\n 	font-family:Arial; ";
            strHTML += "\n 	mso-generic-font-family:auto; ";
            strHTML += "\n 	mso-font-charset:0; ";
            strHTML += "\n 	mso-number-format:General; ";
            strHTML += "\n 	text-align:general; ";
            strHTML += "\n 	vertical-align:bottom; ";
            strHTML += "\n 	border:none; ";
            strHTML += "\n 	mso-background-source:auto; ";
            strHTML += "\n 	mso-pattern:auto; ";
            strHTML += "\n 	mso-protection:locked visible; ";
            strHTML += "\n 	white-space:nowrap; ";
            strHTML += "\n 	mso-rotate:0;} ";
            strHTML += "\n .xl23 ";
            strHTML += "\n 	{mso-style-parent:style0; ";
            strHTML += "\n 		font-weight:1600; ";
            strHTML += "\n 		font-family:Arial, sans-serif; ";
            strHTML += "\n 		mso-font-charset:0; ";
            strHTML += "\n 		mso-pattern:auto none;} ";
            strHTML += "\n 	text-align:center;} ";
            strHTML += "\n .xl24 ";
            strHTML += "\n 	{mso-style-parent:style0; ";
            strHTML += "\n 	    color: white; ";
            strHTML += "\n 		font-weight:1000; ";
            strHTML += "\n 		font-family:Arial, sans-serif; ";
            strHTML += "\n 		mso-font-charset:0; ";
            strHTML += "\n 		text-align:center; ";
            strHTML += "\n 		background:Black; ";
            strHTML += "\n 		mso-pattern:auto none;} ";
            strHTML += "\n 	text-align:center;} ";
            strHTML += "\n 	.xl25 ";
            strHTML += "\n 	{	font-family:Arial, sans-serif; ";
            strHTML += "\n 		mso-font-charset:0; ";
            strHTML += "\n 		text-align:center; ";
            strHTML += "\n 		mso-pattern:auto none;} ";
            strHTML += "\n 	.xl26 ";
            strHTML += "\n 		{mso-style-parent:style0; ";
            strHTML += "\n 		mso-pattern:auto none;} ";
            strHTML += "\n --> ";
            strHTML += "\n </style> ";
            strHTML += "\n <!--[if gte mso 9]><xml> ";
            strHTML += "\n  <x:ExcelWorkbook> ";
            strHTML += "\n   <x:ExcelWorksheets> ";
            strHTML += "\n    <x:ExcelWorksheet> ";
            strHTML += "\n     <x:Name>Sheet1</x:Name> ";
            strHTML += "\n     <x:WorksheetOptions> ";
            strHTML += "\n      <x:Selected/> ";
            strHTML += "\n      <x:Panes> ";
            strHTML += "\n       <x:Pane> ";
            strHTML += "\n        <x:Number>3</x:Number> ";
            strHTML += "\n        <x:ActiveRow>2</x:ActiveRow> ";
            strHTML += "\n        <x:ActiveCol>3</x:ActiveCol> ";
            strHTML += "\n       </x:Pane> ";
            strHTML += "\n      </x:Panes> ";
            strHTML += "\n      <x:ProtectContents>False</x:ProtectContents> ";
            strHTML += "\n      <x:ProtectObjects>False</x:ProtectObjects> ";
            strHTML += "\n      <x:ProtectScenarios>False</x:ProtectScenarios> ";
            strHTML += "\n     </x:WorksheetOptions> ";
            strHTML += "\n    </x:ExcelWorksheet> ";
            strHTML += "\n    <x:ExcelWorksheet> ";
            strHTML += "\n     <x:Name>Sheet2</x:Name> ";
            strHTML += "\n     <x:WorksheetOptions> ";
            strHTML += "\n      <x:ProtectContents>False</x:ProtectContents> ";
            strHTML += "\n      <x:ProtectObjects>False</x:ProtectObjects> ";
            strHTML += "\n      <x:ProtectScenarios>False</x:ProtectScenarios> ";
            strHTML += "\n     </x:WorksheetOptions> ";
            strHTML += "\n    </x:ExcelWorksheet> ";
            strHTML += "\n    <x:ExcelWorksheet> ";
            strHTML += "\n     <x:Name>Sheet3</x:Name> ";
            strHTML += "\n     <x:WorksheetOptions> ";
            strHTML += "\n      <x:ProtectContents>False</x:ProtectContents> ";
            strHTML += "\n      <x:ProtectObjects>False</x:ProtectObjects> ";
            strHTML += "\n      <x:ProtectScenarios>False</x:ProtectScenarios> ";
            strHTML += "\n     </x:WorksheetOptions> ";
            strHTML += "\n    </x:ExcelWorksheet> ";
            strHTML += "\n   </x:ExcelWorksheets> ";
            strHTML += "\n   <x:WindowHeight>9300</x:WindowHeight> ";
            strHTML += "\n   <x:WindowWidth>15135</x:WindowWidth> ";
            strHTML += "\n   <x:WindowTopX>120</x:WindowTopX> ";
            strHTML += "\n   <x:WindowTopY>120</x:WindowTopY> ";
            strHTML += "\n   <x:AcceptLabelsInFormulas/> ";
            strHTML += "\n   <x:ProtectStructure>False</x:ProtectStructure> ";
            strHTML += "\n   <x:ProtectWindows>False</x:ProtectWindows> ";
            strHTML += "\n  </x:ExcelWorkbook> ";
            strHTML += "\n </xml><![endif]--> ";
            strHTML += "\n </head> ";
            strHTML += "\n <body link=blue vlink=purple> ";
            strHTML += "\n <table x:str border=0 cellpadding=0 cellspacing=0 width=640 style='border-collapse: ";
            strHTML += "\n  collapse;table-layout:fixed;width:100%'> ";
            strHTML += strView;
            strHTML += "\n </table> ";
            strHTML += "\n </body> ";
            strHTML += "\n </html> ";
            return strHTML;
        }
        public static bool createFileText(string strPath,string TextView)
        {
            bool boolCheck = false;
            if (!strPath.Equals(""))
            {
                try
                {
                    deleteFile(strPath);
                    FileStream file = new FileStream(strPath, FileMode.Create, FileAccess.Write);
                    StreamWriter sw = new StreamWriter(file);
                    sw.Write(TextView);
                    sw.Close();
                    file.Close();
                    boolCheck = true;
                }
                catch
                {
                    boolCheck = false;
                }
            }
            return boolCheck;
        }
        /*
       Method: Create Folder
       Purpose: Upload Folder truyen vao la duong dan Folder
       Input: bool
       Output: Name of attached file
       */
        public static bool createFolder(string strPath)
        {
            bool boolCheck = false;
            if (!strPath.Equals(""))
            {
                try
                {
                    Directory.CreateDirectory(strPath);
                    boolCheck = true;
                }
                catch
                {
                    boolCheck = false;
                }
            }
            return boolCheck;
        }
        /*
        Method: Delete Folder
        Purpose: Upload Folder truyen vao la duong dan Folder
        Input: bool
        Output: Name of attached file
        */
        public static bool deleteFolder(string strPath)
        {
            bool boolCheck = false;
            if (!strPath.Equals(""))
            {
                try
                {
                    string[] strFiles = Directory.GetFiles(strPath);
                    for (int i = 0; i < strFiles.Length; i++)
                    {
                        deleteFile(strFiles[i]);
                    }
                    Directory.Delete(strPath);
                    boolCheck = true;
                }
                catch
                {
                    boolCheck = false;
                }
            }
            return boolCheck;
        }
        /*
         Method: upLoadFile theo string
         Purpose: Upload file truyen vao la duong dan file
         Input: Virtual path, HttpPostedFile
         Output: Name of attached file
         */
        public static string upLoadFilePath(string strPath, string strPathFile,string strNameFile,ref decimal decSize, bool boolRandom)
        {
            long nFileLen = strPathFile.Length;
            if (nFileLen > 0)
            {
                if (boolRandom)
                {
                    strNameFile = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString()
                        + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()
                        + strNameFile;
                }
                strPath = strPath + strNameFile;
                try
                {
                    byte[] byteFile =  File.ReadAllBytes(strPathFile);
                    decSize = Convert.ToDecimal(byteFile.LongLength);
                    File.WriteAllBytes(strPath, byteFile);
                    File.Exists(strPathFile);
                }
                catch
                {
                    strNameFile = "";
                }
            }
            return strNameFile;
        }
        /*
         Method: upLoadFile
         Purpose: Upload file truyen vao la duong dan file
         Input: Virtual path, HttpPostedFile
         Output: Name of attached file
         */
        public static string upLoadFile(string strPath, HttpPostedFile myUploadFile, bool boolRandom)
        {
            string strFileName = "";
            long nFileLen = myUploadFile.ContentLength;
            if (nFileLen > 0)
            {
                strFileName = Path.GetFileName(myUploadFile.FileName);
                if (boolRandom)
                {
                    int intTemp = strFileName.LastIndexOf(".");
                    strFileName = strFileName.Substring(intTemp);
                    strFileName = DateTime.Now.Day.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Year.ToString()
                        + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString()
                        + strFileName;
                }
                string strTempPath = strPath + strFileName;
                try
                {
                    myUploadFile.SaveAs(strTempPath);
                }
                catch
                {
                    strFileName = "";
                }
            }
            return strFileName;
        }
        /* 
            Method: Delete exits file
        */
        public static bool deleteFile(string strPath)
        {
            if (File.Exists(strPath))
            {
                File.Delete(strPath);
                return true;
            }
            return false;
        }
        public static void SaveFile(ref System.Web.HttpResponse res,DataTable dt,string strNameFile,string strTypeFile)
        {
            //strNameFile = FileName.xls
            //strTypeFile = "application/vnd.xls" or application/vnd.word  or application/vnd.pdf
            //
            res.Clear();
            res.AddHeader("content-disposition", "attachment;filename=" + strNameFile);
            res.ContentEncoding = System.Text.Encoding.UTF8;
            res.Charset = "";
            res.Cache.SetCacheability(HttpCacheability.NoCache);
            res.ContentType = strTypeFile;
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            //dt.RenderControl(htmlWrite);
            //htmlWrite.InnerWriter = "abcbadafa";
            res.Write(stringWrite.ToString());
        }
    }
}